﻿using System;
using System.Collections.Generic;

namespace YArchitech.Plumbing
{
	public class InBuildSystem
	{
		public InBuildSystem()
		{
			this.srpSystems = new List<ShareRPipeSystem>();
			this.supplyTMPipe = new List<HYPipeData>();
			this.returnTMPipe = new List<HYPipeData>();
		}

		public InBuildSystem(InBuildSystem inBuildSystem)
		{
			this.serialNum = inBuildSystem.serialNum;
			this.riserPipeNum = inBuildSystem.riserPipeNum;
			this.srpSystems = this.InitialShareRPipeSystems(inBuildSystem.srpSystems);
			this.supplyTMPipe = this.InitialPipeDatas(inBuildSystem.supplyTMPipe);
			this.returnTMPipe = this.InitialPipeDatas(inBuildSystem.returnTMPipe);
			this.totalLoad = inBuildSystem.totalLoad;
			this.totalFlow = inBuildSystem.totalFlow;
			this.totalResistance = inBuildSystem.totalResistance;
			this.pressCapital = inBuildSystem.pressCapital;
			this.pressActual = inBuildSystem.pressActual;
			this.rateUnbalance = inBuildSystem.rateUnbalance;
			this.pipeConnForm = inBuildSystem.pipeConnForm;
		}

		public void SetConnStyleAndTemper(OptionData option)
		{
			this.PipeConnForm = option.MainPipeInter;
			foreach (ShareRPipeSystem shareRPipeSystem in this.SrpSystems)
			{
				shareRPipeSystem.PipeConnForm = option.RiserPipeInter;
				foreach (FloorSystem floorSystem in shareRPipeSystem.FloorSystems)
				{
					foreach (IndoorSystem indoorSystem in floorSystem.IndoorSystems)
					{
						foreach (CirclePipe circlePipe in indoorSystem.CirclePipes)
						{
							circlePipe.TemperSupply = option.TemperSupply;
							circlePipe.TemperReturn = option.TemperReturn;
						}
					}
				}
			}
		}

		public void CalcRiserPipeLength(OptionData option)
		{
			if (this.RiserPipeNum > 1)
			{
				if (this.PipeConnForm == 1)
				{
					for (int i = 0; i < this.RiserPipeNum - 1; i++)
					{
						this.ReturnTmPipe[i].Length = this.SupplyTmPipe[i + 1].Length;
					}
					this.ReturnTmPipe[0].Length = this.SupplyTmPipe[0].Length;
					for (int j = 1; j < this.RiserPipeNum - 1; j++)
					{
						this.ReturnTmPipe[0].Length += this.SupplyTmPipe[j].Length;
					}
				}
				else
				{
					for (int k = 0; k < this.RiserPipeNum; k++)
					{
						this.ReturnTmPipe[k].Length = this.SupplyTmPipe[k].Length;
					}
				}
			}
			foreach (ShareRPipeSystem shareRPipeSystem in this.SrpSystems)
			{
				IList<HYPipeData> list = shareRPipeSystem.SupplyPipes;
				if (list.Count > 1)
				{
					for (int l = 1; l < list.Count; l++)
					{
						if (l > option.FloorNum)
						{
							list[l].Length = option.FloorList[0].Height;
						}
						else
						{
							list[l].Length = option.FloorList[l - 1].Height;
						}
					}
				}
				list = shareRPipeSystem.ReturnPipes;
				if (shareRPipeSystem.PipeConnForm == 1)
				{
					if (list.Count == 1)
					{
						list[0].Length = shareRPipeSystem.SupplyPipes[0].Length;
					}
					else
					{
						for (int m = 0; m < list.Count - 1; m++)
						{
							if (m >= option.FloorNum)
							{
								list[m].Length = option.FloorList[0].Height;
							}
							else
							{
								list[m].Length = option.FloorList[m].Height;
							}
						}
						list[list.Count - 1].Length = shareRPipeSystem.SupplyPipes[0].Length;
						for (int n = 0; n < list.Count - 1; n++)
						{
							if (n >= option.FloorNum)
							{
								list[list.Count - 1].Length += option.FloorList[0].Height;
							}
							else
							{
								list[list.Count - 1].Length += option.FloorList[n].Height;
							}
						}
					}
				}
				else
				{
					list[0].Length = shareRPipeSystem.SupplyPipes[0].Length;
					for (int num = 1; num < list.Count; num++)
					{
						if (num > option.FloorNum)
						{
							list[num].Length = option.FloorList[0].Height;
						}
						else
						{
							list[num].Length = option.FloorList[num - 1].Height;
						}
					}
				}
			}
		}

		public void ReckonFittingOnPipe()
		{
			if (this.RiserPipeNum == 1)
			{
				this.ReturnTmPipe[0].ValvesList = this.AddElbow(this.ReturnTmPipe[0].ValvesList);
			}
			else if (this.RiserPipeNum > 1)
			{
				for (int i = 1; i < this.RiserPipeNum; i++)
				{
					this.SupplyTmPipe[i].ValvesList = this.AddTeeOrCross(this.SupplyTmPipe[i].ValvesList, ValveString.直流三通);
				}
				if (this.PipeConnForm == 1)
				{
					this.ReturnTmPipe[0].ValvesList = this.AddElbow(this.ReturnTmPipe[0].ValvesList);
					for (int j = 1; j < this.RiserPipeNum; j++)
					{
						this.ReturnTmPipe[j].ValvesList = this.AddTeeOrCross(this.ReturnTmPipe[j].ValvesList, ValveString.合流三通);
					}
					this.ReturnTmPipe[this.RiserPipeNum - 1].ValvesList = this.AddElbow(this.ReturnTmPipe[this.RiserPipeNum - 1].ValvesList);
				}
				else
				{
					for (int k = 0; k < this.RiserPipeNum - 1; k++)
					{
						this.ReturnTmPipe[k].ValvesList = this.AddTeeOrCross(this.ReturnTmPipe[k].ValvesList, ValveString.合流三通);
					}
					this.ReturnTmPipe[this.RiserPipeNum - 1].ValvesList = this.AddElbow(this.ReturnTmPipe[this.RiserPipeNum - 1].ValvesList);
				}
			}
			int num = 0;
			foreach (ShareRPipeSystem shareRPipeSystem in this.SrpSystems)
			{
				if (this.RiserPipeNum == 1 || num == this.SrpSystems.Count - 1)
				{
					shareRPipeSystem.SupplyPipes[0].ValvesList = this.AddElbow(shareRPipeSystem.SupplyPipes[0].ValvesList);
				}
				else
				{
					shareRPipeSystem.SupplyPipes[0].ValvesList = this.AddTeeOrCross(shareRPipeSystem.SupplyPipes[0].ValvesList, ValveString.旁流三通);
				}
				if (shareRPipeSystem.SupplyPipes.Count > 1)
				{
					for (int l = 1; l < shareRPipeSystem.SupplyPipes.Count; l++)
					{
						HYPipeData hypipeData = shareRPipeSystem.SupplyPipes[l];
						if (shareRPipeSystem.FloorSystems[l - 1].IndoorNum == 1)
						{
							hypipeData.ValvesList = this.AddTeeOrCross(hypipeData.ValvesList, ValveString.直流三通);
						}
						else
						{
							hypipeData.ValvesList = this.AddTeeOrCross(hypipeData.ValvesList, ValveString.直流四通);
						}
					}
				}
				num++;
			}
			foreach (ShareRPipeSystem shareRPipeSystem2 in this.SrpSystems)
			{
				if (shareRPipeSystem2.PipeConnForm == 1)
				{
					if (shareRPipeSystem2.FloorSystems[0].IndoorNum == 1)
					{
						shareRPipeSystem2.ReturnPipes[0].ValvesList = this.AddElbow(shareRPipeSystem2.ReturnPipes[0].ValvesList);
					}
					else
					{
						shareRPipeSystem2.ReturnPipes[0].ValvesList = this.AddTeeOrCross(shareRPipeSystem2.ReturnPipes[0].ValvesList, ValveString.合流三通);
					}
					if (shareRPipeSystem2.ReturnPipes.Count > 1)
					{
						for (int m = 1; m < shareRPipeSystem2.ReturnPipes.Count; m++)
						{
							if (shareRPipeSystem2.FloorSystems[m].IndoorNum == 1)
							{
								shareRPipeSystem2.ReturnPipes[m].ValvesList = this.AddTeeOrCross(shareRPipeSystem2.ReturnPipes[m].ValvesList, ValveString.合流三通);
							}
							else
							{
								shareRPipeSystem2.ReturnPipes[m].ValvesList = this.AddTeeOrCross(shareRPipeSystem2.ReturnPipes[m].ValvesList, ValveString.分合流四通);
							}
						}
					}
					shareRPipeSystem2.ReturnPipes[shareRPipeSystem2.ReturnPipes.Count - 1].ValvesList = this.AddElbow(shareRPipeSystem2.ReturnPipes[shareRPipeSystem2.ReturnPipes.Count - 1].ValvesList);
				}
				else
				{
					for (int n = 0; n < shareRPipeSystem2.ReturnPipes.Count - 1; n++)
					{
						if (n == 0)
						{
							if (shareRPipeSystem2.FloorSystems[n].IndoorNum == 1)
							{
								shareRPipeSystem2.ReturnPipes[n].ValvesList = this.AddElbow(shareRPipeSystem2.ReturnPipes[n].ValvesList);
							}
							else
							{
								shareRPipeSystem2.ReturnPipes[n].ValvesList = this.AddTeeOrCross(shareRPipeSystem2.ReturnPipes[n].ValvesList, ValveString.合流三通);
							}
						}
						else if (shareRPipeSystem2.FloorSystems[n].IndoorNum == 1)
						{
							shareRPipeSystem2.ReturnPipes[n].ValvesList = this.AddTeeOrCross(shareRPipeSystem2.ReturnPipes[n].ValvesList, ValveString.合流三通);
						}
						else
						{
							shareRPipeSystem2.ReturnPipes[n].ValvesList = this.AddTeeOrCross(shareRPipeSystem2.ReturnPipes[n].ValvesList, ValveString.分合流四通);
						}
					}
					if (shareRPipeSystem2.ReturnPipes.Count > 1)
					{
						int index = shareRPipeSystem2.ReturnPipes.Count - 1;
						if (shareRPipeSystem2.FloorSystems[index].IndoorNum == 1)
						{
							shareRPipeSystem2.ReturnPipes[index].ValvesList = this.AddElbow(shareRPipeSystem2.ReturnPipes[index].ValvesList);
						}
						else
						{
							shareRPipeSystem2.ReturnPipes[index].ValvesList = this.AddTeeOrCross(shareRPipeSystem2.ReturnPipes[index].ValvesList, ValveString.合流三通);
						}
					}
				}
			}
		}

		private IList<ShareRPipeSystem> InitialShareRPipeSystems(IList<ShareRPipeSystem> srpSystems)
		{
			IList<ShareRPipeSystem> list = new List<ShareRPipeSystem>();
			if (srpSystems == null || srpSystems.Count < 1)
			{
				return list;
			}
			foreach (ShareRPipeSystem shareRPipe in srpSystems)
			{
				ShareRPipeSystem item = new ShareRPipeSystem(shareRPipe);
				list.Add(item);
			}
			return list;
		}

		private IList<HYPipeData> InitialPipeDatas(IList<HYPipeData> pipes)
		{
			IList<HYPipeData> list = new List<HYPipeData>();
			if (pipes == null || pipes.Count < 1)
			{
				return list;
			}
			foreach (HYPipeData hyPipe in pipes)
			{
				HYPipeData item = new HYPipeData(hyPipe);
				list.Add(item);
			}
			return list;
		}

		private IList<ValveAndFitting> AddElbow(IList<ValveAndFitting> valveList)
		{
			if (valveList.Count < 1)
			{
				valveList.Add(new ValveAndFitting(ValveString.弯头.ToString(), 1.0));
			}
			else
			{
				bool flag = false;
				IEnumerator<ValveAndFitting> enumerator = valveList.GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						if (enumerator.Current.VFName == ValveString.弯头.ToString())
						{
							flag = true;
						}
					}
				}
				if (!flag)
				{
					valveList.Add(new ValveAndFitting(ValveString.弯头.ToString(), 1.0));
				}
			}
			return valveList;
		}

		private IList<ValveAndFitting> AddTeeOrCross(IList<ValveAndFitting> valveList, ValveString valveName)
		{
			if (valveList.Count < 1)
			{
				valveList.Add(new ValveAndFitting(valveName.ToString(), 1.0));
			}
			else
			{
				IList<ValveAndFitting> list = new List<ValveAndFitting>();
				foreach (ValveAndFitting valveAndFitting in valveList)
				{
					if (valveAndFitting.VFName.Contains("三通") || valveAndFitting.VFName.Contains("四通"))
					{
						list.Add(valveAndFitting);
					}
				}
				if (list.Count > 0)
				{
					foreach (ValveAndFitting item in list)
					{
						valveList.Remove(item);
					}
				}
				valveList.Add(new ValveAndFitting(valveName.ToString(), 1.0));
			}
			return valveList;
		}

		private double GetLoad()
		{
			double num = 0.0;
			for (int i = 0; i < this.SrpSystems.Count; i++)
			{
				num += this.SrpSystems[i].Load;
				this.SupplyTmPipe[i].Load = 0.0;
				for (int j = i; j < this.SrpSystems.Count; j++)
				{
					this.SupplyTmPipe[i].Load += this.SrpSystems[j].Load;
				}
				this.ReturnTmPipe[i].Load = 0.0;
				if (this.PipeConnForm == 1)
				{
					for (int k = 0; k <= i; k++)
					{
						this.ReturnTmPipe[i].Load += this.SrpSystems[k].Load;
					}
				}
				else
				{
					for (int l = i; l < this.SrpSystems.Count; l++)
					{
						this.ReturnTmPipe[i].Load += this.SrpSystems[l].Load;
					}
				}
			}
			return num;
		}

		private double GetFlow()
		{
			double num = 0.0;
			for (int i = 0; i < this.SrpSystems.Count; i++)
			{
				num += this.SrpSystems[i].Flow;
				this.SupplyTmPipe[i].Flow = 0.0;
				for (int j = i; j < this.SrpSystems.Count; j++)
				{
					this.SupplyTmPipe[i].Flow += this.SrpSystems[j].Flow;
				}
				this.ReturnTmPipe[i].Flow = 0.0;
				if (this.PipeConnForm == 1)
				{
					for (int k = 0; k <= i; k++)
					{
						this.ReturnTmPipe[i].Flow += this.SrpSystems[k].Flow;
					}
				}
				else
				{
					for (int l = i; l < this.SrpSystems.Count; l++)
					{
						this.ReturnTmPipe[i].Flow += this.SrpSystems[l].Flow;
					}
				}
			}
			return num;
		}

		public string SerialNum
		{
			get
			{
				return this.serialNum;
			}
			set
			{
				this.serialNum = value;
			}
		}

		public IList<ShareRPipeSystem> SrpSystems
		{
			get
			{
				return this.srpSystems;
			}
			set
			{
				this.srpSystems = value;
			}
		}

		public int RiserPipeNum
		{
			get
			{
				return this.SrpSystems.Count;
			}
			set
			{
				this.riserPipeNum = value;
			}
		}

		public IList<HYPipeData> SupplyTmPipe
		{
			get
			{
				return this.supplyTMPipe;
			}
			set
			{
				this.supplyTMPipe = value;
			}
		}

		public IList<HYPipeData> ReturnTmPipe
		{
			get
			{
				return this.returnTMPipe;
			}
			set
			{
				this.returnTMPipe = value;
			}
		}

		public int PipeConnForm
		{
			get
			{
				return this.pipeConnForm;
			}
			set
			{
				this.pipeConnForm = value;
			}
		}

		public double TotalLoad
		{
			get
			{
				return this.GetLoad();
			}
			set
			{
				this.totalLoad = value;
			}
		}

		public double TotalFlow
		{
			get
			{
				return this.GetFlow();
			}
			set
			{
				this.totalFlow = value;
			}
		}

		public double TotalResistance
		{
			get
			{
				return this.totalResistance;
			}
			set
			{
				this.totalResistance = value;
			}
		}

		public double PressCapital
		{
			get
			{
				return this.pressCapital;
			}
			set
			{
				this.pressCapital = value;
			}
		}

		public double PressActual
		{
			get
			{
				return this.pressActual;
			}
			set
			{
				this.pressActual = value;
			}
		}

		public double RateUnbalance
		{
			get
			{
				return this.rateUnbalance;
			}
			set
			{
				this.rateUnbalance = value;
			}
		}

		private string serialNum;

		private IList<ShareRPipeSystem> srpSystems;

		private int riserPipeNum;

		private IList<HYPipeData> supplyTMPipe;

		private IList<HYPipeData> returnTMPipe;

		private int pipeConnForm;

		private double totalLoad;

		private double totalFlow;

		private double totalResistance;

		private double pressCapital;

		private double pressActual;

		private double rateUnbalance;
	}
}
